<template>
  <div class="container">
    <div class="com-echarts" ref="rank_ref"></div>
  </div>
</template>

<script>
import "../../public/static/theme/chalk";
export default {
  data() {
    return {
      InitEcharts: null,
      allData: [],
      startValue: 0,
      endValue: 9,
      timeId: null,
    };
  },
  methods: {
    echartsInit() {
      this.InitEcharts = this.$echarts.init(this.$refs.rank_ref, "chalk");
      const initOption = {
        title: {
          text: "| 地区销售排行",
          left: "20vh",
          top: "20vh",
          textStyle: {
            fontSize: "1rem",
          },
        },
        grid: {
          top: "20%",
          right: "5%",
          left: "5%",
          bottom: "5%",
        },
        containLabel: true,
        tooltip: {
          show: true,
        },
        xAxis: {
          type: "category",
        },
        yAxis: {
          type: "value",
        },
        series: [
          {
            type: "bar",
            barWidth: "60%",
            itemStyle: {
              borderRadius: [50, 50, 0, 0],
            },
          },
        ],
      };
      this.InitEcharts.setOption(initOption);
      this.InitEcharts.on("mouseover", () => {
        clearInterval(this.timeId);
      });
      this.InitEcharts.on("mouseout", () => {
        this.startInterval();
      });
    },
    async getData() {
      const { data: res } = await this.$http.get("/rank");
      // console.log(res);
      this.allData = res;
      this.updateCharts();
      this.startInterval();
    },
    updateCharts() {
      //处理图标需要的数据
      const proviceArr = [];
      const valueArr = [];
      this.allData
        .sort((a, b) => {
          return b.value - a.value;
        })
        .forEach((item) => {
          proviceArr.push(item.name);
          valueArr.push(item.value);
        });
      const dataOption = {
        xAxis: {
          data: proviceArr,
        },
        series: [
          {
            data: valueArr,
            itemStyle: {
              color: (e) => {
                // console.log(e)
                if (e.value > 300) {
                  return {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: "#0BA82C", // 0% 处的颜色
                      },
                      {
                        offset: 1,
                        color: "#4FF778", // 100% 处的颜色
                      },
                    ],
                    global: false, // 缺省为 false
                  };
                }
                if (e.value > 200) {
                  return {
                    type: "linear",
                    x: 0,
                    y: 0,
                    x2: 0,
                    y2: 1,
                    colorStops: [
                      {
                        offset: 0,
                        color: "#2E72BF", // 0% 处的颜色
                      },
                      {
                        offset: 1,
                        color: "#23E5E5", // 100% 处的颜色
                      },
                    ],
                    global: false, // 缺省为 false
                  };
                }
                return {
                  type: "linear",
                  x: 0,
                  y: 0,
                  x2: 0,
                  y2: 1,
                  colorStops: [
                    {
                      offset: 0,
                      color: "#5052EE", // 0% 处的颜色
                    },
                    {
                      offset: 1,
                      color: "#AB6EE5", // 100% 处的颜色
                    },
                  ],
                  global: false, // 缺省为 false
                };
              },
            },
          },
        ],
        dataZoom: {
          show: false,
          startValue: this.startValue,
          endValue: this.endValue,
        },
      };
      this.InitEcharts.setOption(dataOption);
    },
    screenAdapte() {
      const titleFontSize = (this.$refs.seller_ref.offsetWidth / 100) * 3.6;
      const adapteOption = {
        title: {
          textStyle: {
            fontSize: titleFontSize,
          },
        },
      };
      this.InitEcharts.setOption(adapteOption);
      this.InitEcharts.resize();
    },
    startInterval() {
      this.timeId = setInterval(() => {
        this.startValue++;
        this.endValue++;
        if (this.endValue > this.allData.length - 1) {
          this.startValue = 0;
          this.endValue = 9;
        }
        this.updateCharts();
      }, 2000);
    },
  },
  created() {
    this.getData();
  },
  mounted() {
    this.echartsInit();
    window.addEventListener("resize", this.screenAdapte);
    this.screenAdapte();
  },
  destroyed() {
    window.removeEventListener("resize", this.screenAdapte);
    clearInterval(this.timeId);
  },
};
</script>

<style lang="less" scoped>
.container {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;

  .com-echarts {
    width: 100%;
    height: 100%;
    margin: 0;
    padding: 0;
    overflow: hidden;
  }
}
</style>